class_name Enemy
extends Unit

@export var flock_push := 20.0

@onready var visual_area: Area2D = $VisualArea
@onready var knockback_timer: Timer = %KnockbackTimer

var can_move := true

var knockback_dir := Vector2.ZERO
var knockback_power := 0.0

func _process(delta: float) -> void:
	if Global.game_paused: return
	if not can_move:
		return
		
	if not can_move_towards_player():
		return
		
	position += (get_move_direction() + knockback_dir * knockback_power) * stats.speed * delta
	update_rotation()
	
func update_rotation() -> void:
	if not is_instance_valid(Global.player):
		return
	var player_pos := Global.player.global_position
	var move_right := global_position.x < player_pos.x
	visuals.scale = Vector2(-0.5, 0.5) if move_right else Vector2(0.5, 0.5)
	
func get_move_direction() -> Vector2:
	if not is_instance_valid(Global.player):
		return Vector2.ZERO
	var direction := global_position.direction_to(Global.player.global_position)
	for area in visual_area.get_overlapping_areas():
		if area != self and area.is_inside_tree():
			var vector := global_position - area.global_position
			direction += flock_push * vector.normalized() / vector.length()
	return direction
		
func can_move_towards_player() -> bool:
	return is_instance_valid(Global.player) and global_position.distance_to(Global.player.global_position) > 60
	
	
func apply_knockback(context_knockback_dir: Vector2, context_knockback_power: float) -> void:
	knockback_dir = context_knockback_dir
	knockback_power = context_knockback_power
	if knockback_timer.time_left > 0:
		knockback_timer.stop()
		reset_knockback()
	knockback_timer.start()
	
func reset_knockback() -> void:
	knockback_dir = Vector2.ZERO
	knockback_power = 0.0

func destroy_enemy() -> void:
	can_move = false
	animation_player.play("die")
	await  animation_player.animation_finished
	queue_free()

func _on_knockback_timer_timeout() -> void:
	reset_knockback()

func _on_hurtbox_component_on_damage(hitbox: HitboxComponent) -> void:
	super._on_hurtbox_component_on_damage(hitbox)
	if hitbox.knockback_power > 0:
		var dir := hitbox.source.global_position.direction_to(global_position)
		apply_knockback(dir, hitbox.knockback_power)


func _on_health_component_on_unit_die() -> void:
	Global.enemy_died(self)
